package org.black.missedCall2.reply.sqlite;

import org.black.missedCall2.reply.ReplyConstant;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class ReplyOpenHelper extends SQLiteOpenHelper {
	private static final int DATABASE_VERSION = 1;
	private static final String DATABASE_NAME = "replyMissedCall";

	public static final String TABLE_REPLY = "MISSEDREPLY";
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_LISTEN_FOR = "listenFor";
	public static final String COLUMN_RESPONSE_TO = "responseTo";
	public static final String COLUMN_MESSAGE = "message";
	public static final String COLUMN_SHOULD_REPLY = "shouldReply";

	private static final String CREATE_SCRIPT = "CREATE TABLE " + TABLE_REPLY
			+ "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
			+ COLUMN_LISTEN_FOR + " INTEGER NOT NULL, " + COLUMN_RESPONSE_TO
			+ " INTEGER NOT NULL, " + COLUMN_MESSAGE + " TEXT NOT NULL, "
			+ COLUMN_SHOULD_REPLY + " INTEGER NOT NULL DEFAULT 1 " + ")";

	public ReplyOpenHelper(Context context, String name, CursorFactory factory,
			int version) {
		super(context, DATABASE_NAME, factory, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_SCRIPT);
		Log.d(ReplyConstant.REPLY_TAG, "Create " + TABLE_REPLY + " table.");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(ReplyConstant.REPLY_TAG, "Upgrading database from version "
				+ oldVersion + " to " + newVersion
				+ ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_REPLY);
		onCreate(db);
	}

}
